package com.liujit.upms.controller;

import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.upms.domain.params.SysRoleParams;
import com.liujit.upms.domain.params.SysRolePermParams;
import com.liujit.upms.domain.query.SysRoleQuery;
import com.liujit.upms.domain.result.SysRolePermResult;
import com.liujit.upms.domain.result.SysRoleResult;
import com.liujit.upms.service.SysRoleService;
import com.liujit.validator.group.AddGroup;
import com.liujit.validator.group.ModifyGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 系统角色管理接口
 * @Author liujun
 * @Date 2021-07-02 15:01:11
 */
@RequiredArgsConstructor
@Api(tags = "系统角色管理接口")
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @ApiOperation(value = "分页查询", notes = "根据系统角色分页查询条件，分页查询系统角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "系统角色分页查询条件", dataType = "SysRoleQuery", dataTypeClass = SysRoleQuery.class, paramType = "query") })
    @GetMapping("/getPage")
    public PageBean<SysRoleResult> getPage(SysRoleQuery query) {
        return sysRoleService.getPage(query);
    }

    @ApiOperation(value = "添加", notes = "根据系统角色添加请求参数，添加系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统角色添加请求参数", dataType = "SysRoleParams", dataTypeClass = SysRoleParams.class, paramType = "body") })
    @PostMapping("add")
    public Boolean add(@RequestBody @Validated(AddGroup.class) SysRoleParams params) {
        return sysRoleService.save(params);
    }

    @ApiOperation(value = "修改", notes = "根据系统角色修改请求参数，修改系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统角色修改请求参数", dataType = "SysRoleParams", dataTypeClass = SysRoleParams.class, paramType = "body") })
    @PostMapping("/modify")
    public Boolean modify(@RequestBody @Validated(ModifyGroup.class) SysRoleParams params) {
        return sysRoleService.save(params);
    }

    @ApiOperation(value = "查询", notes = "根据系统角色id，查询系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "系统角色id", dataType = "Long", dataTypeClass = Long.class, paramType = "query") })
    @GetMapping("/get")
    public SysRoleResult get(Long id) {
        return sysRoleService.get(id);
    }

    @ApiOperation(value = "查询详情", notes = "根据系统角色id，查询系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "系统角色id", dataType = "Long", dataTypeClass = Long.class, paramType = "query") })
    @GetMapping("/getDetail")
    public SysRoleResult getDetail(Long id) {
        return sysRoleService.getDetail(id);
    }

    @ApiOperation(value = "删除", notes = "根据系统角色删除请求参数，删除系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统角色删除请求参数", dataType = "BaseParams", dataTypeClass = BaseParams.class, paramType = "body") })
    @PostMapping("/del")
    public Boolean del(@RequestBody BaseParams params) {
        return sysRoleService.del(params);
    }

    @ApiOperation(value = "启用", notes = "根据系统角色启用请求参数，启用系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统角色启用请求参数", dataType = "BaseParams", dataTypeClass = BaseParams.class, paramType = "body") })
    @PostMapping("/enabled")
    public Boolean enabled(@RequestBody BaseParams params) {
        return sysRoleService.enabled(params);
    }

    @ApiOperation(value = "禁用", notes = "根据系统角色禁用请求参数，禁用系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统角色禁用请求参数", dataType = "BaseParams", dataTypeClass = BaseParams.class, paramType = "body") })
    @PostMapping("/disabled")
    public Boolean disabled(@RequestBody BaseParams params) {
        return sysRoleService.disabled(params);
    }

    @ApiOperation(value = "查询条数", notes = "根据系统机构id、系统角色名称查询系统角色条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", dataType = "Long", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "系统机构id", dataType = "Long", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "系统角色名称", dataType = "String", dataTypeClass = String.class, paramType = "query") })
    @GetMapping("/getCountByName")
    public Integer getCountByName(Long id, Long orgId, String name) {
        return sysRoleService.getCountByName(id, orgId, name);
    }

    @ApiOperation(value = "查询机构角色", notes = "根据系统机构id，查询系统角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "系统机构id", dataType = "String", dataTypeClass = String.class, paramType = "query") })
    @GetMapping("/getListByOrgId")
    public List<SysRoleResult> getListByOrgId(Long orgId) {
        return sysRoleService.getListByOrgId(orgId);
    }

    @ApiOperation(value = "查询角色id", notes = "根据菜单id、URL权限标识，查询拥有当前URL权限标识的角色id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单id", dataType = "Long", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "urlPermission", value = "URL权限标识", dataType = "String", dataTypeClass = String.class, paramType = "query") })
    @GetMapping("/getRoleIdByUrlPermission")
    public List<Long> getRoleIdByUrlPermission(Long menuId, String urlPermission) {
        return sysRoleService.getRoleIdByUrlPermission(menuId, urlPermission);
    }

    @ApiOperation(value = "查询系统角色菜单、按钮权限列表", notes = "根据系统角色id，查询系统角色菜单、按钮权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "系统角色id", dataType = "Long", dataTypeClass = Long.class, paramType = "query") })
    @GetMapping("/getPermByRoleId")
    public SysRolePermResult getPermByRoleId(Long roleId) {
        return sysRoleService.getPermByRoleId(roleId);
    }

    @ApiOperation(value = "修改角色菜单、按钮权限", notes = "根据系统角色菜单、按钮权限修改请求参数、修改系统角色权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "系统角色菜单、按钮权限修改请求参数", dataType = "SysRolePermissionParams", dataTypeClass = SysRolePermParams.class, paramType = "body") })
    @PostMapping("/modifyPerm")
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyPerm(@RequestBody SysRolePermParams params) {
        return sysRoleService.modifyPerm(params);
    }
}
